package com.ciaojian.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciaojian.client.service.OrderCommentServiceImpl;
import com.ciaojian.client.service.OrderServiceImpl;
import com.ciaojian.client.service.StoreServiceImpl;
import com.ciaojian.client.service.WorkerServiceImpl;
import com.ciaojian.core.constant.RedisConstant;
import com.ciaojian.core.model.OrderComment;
import com.ciaojian.core.model.Store;
import com.ciaojian.core.model.dto.WorkerDTO;
import com.ciaojian.core.model.vo.WorkerVo;
import com.ciaojian.core.util.RedisUtil;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 发型师接口
 *
 * @author Atlakyn
 */
@Api(tags = "发型师")
@Slf4j
@Validated
@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Resource
    private WorkerServiceImpl workerService;
    @Resource
    private StoreServiceImpl storeService;
    @Resource
    private OrderCommentServiceImpl orderCommentService;
    @Resource
    private OrderServiceImpl orderService;

    @ApiOperation("查询发型师所属门店")
    @GetMapping("/store")
    public Result getStoreByWorker(@NotNull(message = "发型师ID不能为空") Integer workerId) {
        return Result.ok(storeService.getStoreByWorker(workerId));
    }

    @ApiOperation("根据名字查询发型师")
    @ApiResponses({
            @ApiResponse(code = 200, message = "success", response = WorkerDTO.class)
    })
    @GetMapping("/name")
    public Result getById(String name) {
        Assert.hasText(name, "发型师名称不能为空");

        return Result.ok(workerService.getBaseMapper().selectByName(name));
    }

    /**
     * @param storeId 店铺id
     * @return 店铺下的发型师
     */
    @ApiOperation("获取店铺下的发型师")
    @GetMapping("/workerAll")
    public Result getWorkerById(@NotNull Integer storeId) {
        try {
            Store store = storeService.getById(storeId);


            List<WorkerVo> workerList = workerService.getBaseMapper().getWorkerById(storeId);


            String discount = storeService.getBaseMapper().getDiscount();
            for (WorkerVo vo : workerList) {
                // 查询发型师正在排队的订单
                Integer servicingNum = orderService.countServicingNum(vo.getWid());

                vo.setServicingNum(servicingNum);

                Integer type = workerService.getBaseMapper().getType(vo.getWid() + "");
                Map<String, Integer> map = orderService.minuteTake(vo.getWid(), null);
                vo.setWaitMessage(map);
                // 全部评价数
                QueryWrapper<OrderComment> wrapper = new QueryWrapper<>();
                wrapper.select("count(1) as score").in("score", 4, 5);
                //查询门店评价时
                if (vo.getWid() != null) {
                    wrapper.eq("worker_id", vo.getWid());
                }
                // 查询发型师评价时
                OrderComment all = orderCommentService.getOne(wrapper);
                vo.setComments(all.getScore());
                if (type == 1) {
                    vo.setPrice("48");
                } else if (type == 2) {
                    vo.setPrice("28");
                } else {
                    vo.setPrice("38");
                }
                vo.setDiscount(discount);
                vo.setDinnerDate(RedisUtil.getKey(RedisConstant.WORKER_DINNER + vo.getWid()));
                vo.setAfternoonTeaDate(RedisUtil.getKey(RedisConstant.WORKER_AFTERNOON_TEA + vo.getWid()));
            }
            store.setDWorkerList(workerList);
            return Result.ok(store);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("信息错误");
        }
    }

    @ApiOperation("查询门店下某个班次未排班的发型师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "日期"),
            @ApiImplicitParam(name = "rosterId", value = "班次id"),
            @ApiImplicitParam(name = "storeId", value = "门店id"),
    })
    @GetMapping("/not-schedule")
    public Result getNotSchedule(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date, Integer rosterId, Integer storeId) {
        Assert.notNull(date, "日期不能为空");
        Assert.notNull(storeId, "门店id不能为空");
        return Result.ok(workerService.findNotSchedule(date, rosterId, storeId));
    }

}
